/**
 * Implementacion de la clase Parser.
 */

#include <iostream>
#include <fstream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include "../logger/logger.hpp"
#include "parser.hpp"
#include "../config/config.hpp"

using namespace std;

Parser::Parser(){}

Parser::Parser(std::string &file_parse, Logger *log)
{
	this->create(file_parse,log);
}

void Parser::create(std::string &file_parse, Logger *log)
{
	this->quantity		= 0;
	this->logger 		= log;
	int success_parse	= 0;
	std::string line;
	
	// Pruebo abrir el archivo a parsear
	this->file_to_parse.open(file_parse.c_str());
	if(!this->file_to_parse)
	{
		this->logger->reportErrorMessage("Parser","constructor","No se pudo abrir el archivo a parsear");
	}
	else{	success_parse = 1;	}

	this->separator = ';';

	if(success_parse)
		this->logger->reportSuccessMessage("Parser","constructor","Parser creado");
	else
		this->logger->reportErrorMessage("Parser","constructor","No se pudo crear el Parser");
}

Parser::~Parser()
{
	this->file_to_parse.close();
	this->quantity 	= 0;
	this->separator	= 0;
	this->line.clear();
	this->logger->reportSuccessMessage("Parser","destructor","Parser destruido");
}

int Parser::nextLine()
{
	int i			= 0;
	int success 	= 0;
	this->quantity  = 0;

	if (getline(this->file_to_parse, this->line) != NULL)
	{
		this->quantity = 1;
		
		while(this->line[i] != '\0')
		{
			if(this->line[i] == this->separator)
				this->quantity++;
			if(this->line[i] != '\0')
				i++;
		}	

		/*
		std::string::iterator it = this->line.begin();
		while( it != this->line.end())
		{
			if(*it == this->separator)
			{
				this->quantity++;
			}
			if( it != this->line.end())
				it++;

		}
		*/
	}

	if(this->quantity > 0)
	{
		this->logger->reportSuccessMessage("Parser","next","Linea leida");
		success = 1;
	}
	else
	{
		this->logger->reportErrorMessage("Parser","next","Error al leer linea, Fin de Archivo");
		this->quantity--;
		success = 0;
	}

	return success;
}

int Parser::getNumberOfFields()
{
	return this->quantity;
}

int Parser::getField(int number, std::string &field)
{
	int count 	= 1;
	int success = 1;
	int	i		= 0;


	if(number > this->quantity)
		success = 0;

	if(success)
	{
		
		while((this->line[i] != '\0')&&(count != number))
		{
			if(this->line[i] == this->separator)
				count++;
			i++;
		}

		while((this->line[i] != '\0')&&(this->line[i] != this->separator))
		{
			field.push_back(this->line[i]);
			i++;
		}

		/*
		std::string::iterator it = this->line.begin();
		while((it != this->line.end()) && (count != number))
		{
			if(*it == this->separator)
				count++;
			it++;
		}

		while((it != this->line.end()) && (*it != this->separator))
		{
			field.push_back(*it);
			it++;
		}
		*/

		this->logger->reportSuccessMessage("Parser","getField","Campo obenido");
	}

	return success;
}

void Parser::setSeparator()
{
	std::string sep = Config::get("separator");

	if(sep.c_str() == NULL)
	{
		std::string detail = "Separador para el parser no tiene el formato esperado, se deja el actual";
		this->logger->reportErrorMessage("Parser","setSeparator",detail);
	}
	else
		this->separator = sep[0];
}
